package com.hd.tiyu.yuejian.Fragment;


import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View.OnClickListener;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hd.tiyu.yuejian.R;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ShouyeFragment extends Fragment implements OnClickListener {
    private View view;
    private ViewPager mViewPaper;
    private List<ImageView> images;
    private List<View> dots;
    private int currentItem;
    //存放图片的id  
    private int[]imageIds =new int[]{
            R.drawable.l1,
            R.drawable.l2
    };
    private ViewPagerAdapter adapter;
    private ScheduledExecutorService scheduledExecutorService;

    private LinearLayout HuoDong;
    private LinearLayout DaShi;
    private LinearLayout BiSai;

    private TextView texthuodong;
    private TextView textdashi;
    private TextView textbisai;

    private Fragment Tabhuodong;
    private Fragment Tabdashi;
    private Fragment Tabbisai;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.shouyetab,container,false);
        mViewPaper =(ViewPager)view.findViewById(R.id.mViewPager);
        //显示图片
        images= new ArrayList<ImageView>();
        for(int i=0;i<imageIds.length;i++){
            ImageView imageView=new ImageView(view.getContext());
            imageView.setBackgroundResource(imageIds[i]);
            images.add(imageView);
        }
        adapter=new ViewPagerAdapter();
        mViewPaper.setAdapter(adapter);
        initView();
        initEvent();
        setSelect(0);
        return view;
    }

    //图片的滑动效果
    private class ViewPagerAdapter extends PagerAdapter {

        /**
         * 滑动图片
         */
        @Override
        public int getCount() {
            return images.size();
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position));
            return images.get(position);
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        scheduledExecutorService= Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleWithFixedDelay(
                new ViewPageTask(),
                5,
                5,
                TimeUnit.SECONDS);
    }
    //图片轮播任务
    private class ViewPageTask implements Runnable{
        @Override
        public void run() {
            currentItem=(currentItem+1)%imageIds.length;
            mHandle.sendEmptyMessage(0);
        }
    }
    private Handler mHandle=new Handler(){
        public void handleMessage(android.os.Message message){
            mViewPaper.setCurrentItem(currentItem);
        }
    };
    @Override
    public void onPause() {
        super.onPause();
        if(scheduledExecutorService!=null){
            scheduledExecutorService.shutdown();
            scheduledExecutorService=null;
        }
    }

    private void initEvent(){
        HuoDong.setOnClickListener(this);
        DaShi.setOnClickListener(this);
        BiSai.setOnClickListener(this);
    }
    private void initView(){
        HuoDong=view.findViewById(R.id.tab_huodong);
        DaShi=view.findViewById(R.id.tab_dashi);
        BiSai=view.findViewById(R.id.tab_bisai);

        texthuodong=view.findViewById(R.id.text_huodong);
        textdashi=view.findViewById(R.id.text_dashi);
        textbisai=view.findViewById(R.id.text_bisai);
    }
    @TargetApi(Build.VERSION_CODES.M)
    private void setSelect(int i){
        FragmentManager fragmentManager=getFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                if(Tabhuodong==null){
                    Tabhuodong=new HuodongFragment();
                    transaction.add(R.id.shouye_content,Tabhuodong);
                }else {
                    transaction.show(Tabhuodong);
                }
                texthuodong.setTextColor(getActivity().getColor(R.color.lv));
                break;
            case 1:
                if(Tabdashi==null){
                    Tabdashi=new DashiFtagment();
                    transaction.add(R.id.shouye_content,Tabdashi);
                }else {
                    transaction.show(Tabdashi);
                }
                textdashi.setTextColor(getActivity().getColor(R.color.lv));
                break;
            case 2:
                if(Tabbisai==null){
                    Tabbisai=new BisaiFragment();
                    transaction.add(R.id.shouye_content,Tabbisai);
                }else {
                    transaction.show(Tabbisai);
                }
                textbisai.setTextColor(getActivity().getColor(R.color.lv));
                break;
        }
        transaction.commit();
    }
    private void hideFragment(FragmentTransaction transaction){
        if(Tabhuodong!=null){
            transaction.hide(Tabhuodong);
        }
        if(Tabdashi!=null){
            transaction.hide(Tabdashi);
        }
        if(Tabbisai!=null){
            transaction.hide(Tabbisai);
        }
    }
    @Override
    public void onClick(View view) {
        resetImgs();
        switch (view.getId()){
            case R.id.tab_huodong:
                setSelect(0);
                break;
            case R.id.tab_dashi:
                setSelect(1);
                break;
            case R.id.tab_bisai:
                setSelect(2);
                break;
        }
    }
    @TargetApi(Build.VERSION_CODES.M)
    private void resetImgs(){
        texthuodong.setTextColor(getActivity().getColor(R.color.bai));
        textdashi.setTextColor(getActivity().getColor(R.color.bai));
        textbisai.setTextColor(getActivity().getColor(R.color.bai));
    }
}
